package com.ruoyi.develop.goodsInvoiceInfo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsInvoice.service.impl.GoodsInvoiceServiceImpl;
import com.ruoyi.develop.projectBaseInfo.service.impl.ProjectBaseInfoServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsInvoiceInfo.mapper.GoodsInvoiceInfoMapper;
import com.ruoyi.develop.goodsInvoiceInfo.domain.GoodsInvoiceInfo;
import com.ruoyi.develop.goodsInvoiceInfo.service.IGoodsInvoiceInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 开票明细信息Service业务层处理
 *
 * @author qh
 * @date 2023-12-05
 */
@Service
public class GoodsInvoiceInfoServiceImpl implements IGoodsInvoiceInfoService {
    @Resource
    private GoodsInvoiceInfoMapper goodsInvoiceInfoMapper;
    @Resource
    private ProjectBaseInfoServiceImpl projectBaseInfoService;
    @Resource
    private GoodsInvoiceServiceImpl invoiceService;

    /**
     * 查询开票明细信息
     *
     * @param id 开票明细信息主键
     * @return 开票明细信息
     */
    @Override
    public GoodsInvoiceInfo selectById(String id) {
        return goodsInvoiceInfoMapper.selectById(id);
    }

    /**
     * 查询开票明细信息列表
     *
     * @param entity 开票明细信息
     * @return 开票明细信息
     */
    @Override
    public List<GoodsInvoiceInfo> selectList(GoodsInvoiceInfo entity) {
        return goodsInvoiceInfoMapper.selectList(entity);
    }

    /**
     * 新增开票明细信息
     *
     * @param entity 开票明细信息
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public AjaxResult insert(GoodsInvoiceInfo entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if (StringUtils.isBlank(entity.getNos())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(new Date());
            String nos = goodsInvoiceInfoMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(nos)){
                // 最大编号+1
                nos = Long.parseLong(nos)+1+"";
            }else{
                nos = today+"0001";
            }
            entity.setNos(nos);
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        goodsInvoiceInfoMapper.insert(entity);
        if(StringUtils.isNotEmpty(entity.getProjectId())){
            projectBaseInfoService.updateProjectTaxInfo(entity.getProjectId());
        }
        invoiceService.updateMoney(entity.getMainId());
        return AjaxResult.success("操作成功");
    }

    /**
     * 修改开票明细信息
     *
     * @param entity 开票明细信息
     * @return 结果
     */
    @Override
    public int update(GoodsInvoiceInfo entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsInvoiceInfoMapper.update(entity);
    }

    /**
     * 批量删除开票明细信息
     *
     * @param ids 需要删除的开票明细信息主键
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int deleteByIds(String[] ids) {
        int i= goodsInvoiceInfoMapper.deleteByIds(ids);
        GoodsInvoiceInfo goodsInvoiceInfo = selectById(ids[0]);
        if (goodsInvoiceInfo != null) {
            if(StringUtils.isNotEmpty(goodsInvoiceInfo.getProjectId())){//更新项目相关信息
                projectBaseInfoService.updateProjectTaxInfo(goodsInvoiceInfo.getProjectId());
            }
            invoiceService.updateMoney(goodsInvoiceInfo.getMainId());
        }
        return i;
    }

    /**
     * 删除开票明细信息信息
     *
     * @param id 开票明细信息主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsInvoiceInfoMapper.deleteById(id);
    }
}
